package com.tx.controller;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.ObjectUtil;
import com.tx.entity.User;
import com.tx.service.IUserService;
import com.tx.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RegisterController {

    @Autowired
    private IUserService userService;


    @GetMapping("/register")
    public String register(){
        return "/register";
    }


    @ResponseBody
    @PostMapping("/register")
    public AjaxResult login(String username,String mobile, String password) {


        User user = userService.selectUserByUserName(username);

        if (ObjectUtil.isNotNull(user)){
            return AjaxResult.error("用户已存在,请更换用户名");
        }

        //保存用户
        User registerUser = new User();
        registerUser.setUserName(username);
        registerUser.setUserPassword(password);
        registerUser.setUserMobile(mobile);
        int row = userService.registerUser(registerUser);
        return AjaxResult.toAjax(row);
    }

}
